package com.start.workflow.core.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.workflow.core.manage.StartActInstManage;
import com.start.workflow.core.manage.StartActModelManage;
import com.start.workflow.core.service.StartActBusinessService;
import com.start.workflow.entity.StartActBusinessEntity;
import com.start.workflow.vo.StartActiviti;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "start流程API")
@RestController
@RequestMapping("/startActApi")
public class StartActApi {
    @Autowired
    private StartActInstManage actInstManage;

    @Autowired
    private StartActModelManage startActModelManage;

    @Autowired
    private StartActBusinessService startActBusinessService;

    @Operation(summary = "分页查询我的申请")
    @GetMapping("/pageMyBusiness")
    public Result<?> pageMyBusiness(Page<StartActBusinessEntity> page) {
        QueryWrapper<StartActBusinessEntity> queryWrapper = DataQueryConstructor.createEntity(StartActBusinessEntity.class);
        return Result.ok(startActBusinessService.pageMyBusiness(page, queryWrapper));
    }

    @Operation(summary = "获取流程定义详情")
    @GetMapping("/getStartProcDefDetail/{procDefKey}")
    public Result<?> getStartProcDefDetail(@PathVariable("procDefKey") String procDefKey, @RequestParam(defaultValue = "false") boolean loadFormOption) {
        return Result.ok(startActModelManage.getStartProcDefDetail(procDefKey, loadFormOption));
    }

    /**
     * 启动流程
     *
     * @return
     */
    @Operation(summary = "启动流程")
    @PostMapping("/startApply/{procDefKey}")
    public Result<?> startApply(@PathVariable("procDefKey") String procDefKey, @RequestBody StartActiviti startAct) {
        actInstManage.startAct(procDefKey, startAct);
        return Result.ok();
    }

    /**
     * 获取申请详情
     *
     * @param actBusinessId
     * @return
     */
    @Operation(summary = "获取流程详情")
    @GetMapping("/getActInsDetail/{actBusinessId}")
    public Result<?> getActInsDetail(@PathVariable("actBusinessId") String actBusinessId, @RequestParam("false") boolean loadFormOption) {
        return Result.ok(actInstManage.getActInsDetail(actBusinessId, loadFormOption));
    }


    /**
     * 取消流程
     *
     * @return
     */
    @Operation(summary = "取消申请流程")
    @PostMapping("/cancelApply/{actBusinessId}")
    public Result<?> cancelApply(@PathVariable("actBusinessId") String actBusinessId, @RequestBody String reason) {
        actInstManage.cancelApply(actBusinessId, reason);
        return Result.ok();
    }

    @Operation(summary = "删除流程")
    @DeleteMapping("/deleteActById/{actBusinessId}")
    public Result<?> deleteActById(@PathVariable("actBusinessId") String actBusinessId) {
        actInstManage.deleteActIns(actBusinessId);
        return Result.ok();
    }

}
